namespace SimpleWebServices.Configuration
{
    using System;
    using System.Collections.ObjectModel;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Xml.XPath;
    using SimpleWebServices;
    using Xunit;

    public sealed class RedirectionCollectionFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<RedirectionCollection>()
                .DerivesFrom<Collection<RedirectionSetting>>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("redirections")
                .Implements<IXmlSerializable>()
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            RedirectionCollection actual = (
                "<redirections>" +
                    "<redirect from='/foo' to='/bar' />" +
                "</redirections>").Deserialize<RedirectionCollection>();

            Assert.Equal<string>("/foo", actual[0].From);
            Assert.Equal<string>("/bar", actual[0].To);
        }

        [Fact]
        public void deserializeEmpty()
        {
            RedirectionCollection actual = "<redirections />".Deserialize<RedirectionCollection>();

            Assert.Empty(actual);
        }

        [Fact]
        public void serialize()
        {
            RedirectionCollection obj = new RedirectionCollection
            {
                new RedirectionSetting
                {
                    From = "/foo",
                    To = "/bar"
                }
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("1=count(/redirections/redirect[@from='/foo'][@to='/bar'])"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new RedirectionCollection());
        }

        [Fact]
        public void indexer_string()
        {
            RedirectionCollection obj = new RedirectionCollection
            {
                new RedirectionSetting
                {
                    From = "/foo",
                    To = "/bar"
                }
            };

            Assert.Equal<string>("/bar", obj["/foo"].To);
        }

        [Fact]
        public void indexer_stringNotFound()
        {
            RedirectionCollection obj = new RedirectionCollection
            {
                new RedirectionSetting
                {
                    From = "/foo",
                    To = "/bar"
                }
            };

            Assert.Null(obj["/not-found"]);
        }

        [Fact]
        public void IXmlSerializable_op_GetSchema()
        {
            Assert.Throws<NotSupportedException>(() => (new RedirectionCollection() as IXmlSerializable).GetSchema());
        }

        [Fact]
        public void IXmlSerializable_op_ReadXml_XmlReaderNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new RedirectionCollection() as IXmlSerializable).ReadXml(null as XmlReader));
        }

        [Fact]
        public void IXmlSerializable_op_WriteXml_XmlWriterNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new RedirectionCollection() as IXmlSerializable).WriteXml(null as XmlWriter));
        }
    }
}